﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>Type - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The Type function returns the exact type of a value." />
<meta name="ahk:equiv-v1" content="AutoHotkey.htm" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>Type</h1>
<p>返回值的确切类型.</p>
<pre class="Syntax">Type := <span class="func">Type</span>(Value)</pre>

<h2 id="Return_Value">返回值</h2>
<p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
<p>函数返回 <em>Value</em> 的类型. 对于原始值, 返回值是以下之一:</p>
<ul>
  <li>String(字符串)</li>
  <li>Integer(整数)</li>
  <li>Float(浮点数)</li>
</ul>
<p>如果 <em>Value</em> 是一个对象, 那么返回值就是对象的类名.</p>
<p>对于<a href="../objects/Object.htm">对象</a>类型, 类名是动态的, 使用如下算法确定:</p>
<pre>
Object_Type(Value) {
    if HasProp(Value, "__Class")  <em>; Ensures classes aren't reported as an instance of the base class.</em>
        return "Class"
    while Value := Value.base
        if HasProp(Value, "__Class")
            return Value.__Class
    return "Object"
}
</pre>
<p>However, unlike this function, the Type function does not cause the object's meta-functions to be called.</p>
<p>For COM wrapper objects, the return value depends on the wrapped <a href="ComObjType.htm">variant type</a>. It is determined as follows:</p>
<pre>
ComObject_Type(obj)
{
    if ComObjType(obj) &amp; 0x2000 <em>; VT_ARRAY</em>
        return "<a href="ComObjArray.htm">ComObjArray</a>"
    if ComObjType(obj) &amp; 0x4000 <em>; VT_BYREF</em>
        return "<a href="ComObject.htm#ByRef">ComObjRef</a>"
    if (ComObjType(obj) = 9 || ComObjType(obj) = 13) <em>; VT_DISPATCH || VT_UNKNOWN</em>
        &amp;&amp; ComObjValue(obj) != 0
    {
        if (classname := <a href="ComObjType.htm">ComObjType</a>(obj, "Class")) != ""
            return classname
        if ComObjType(obj) = 9 <em>; VT_DISPATCH</em>
            return "ComObject"
    }
    return "ComObj"
}
</pre>

<h2 id="Remarks">备注</h2>
<p>This function typically shouldn't be used to determine if a value is numeric, since numeric <em>strings</em> are valid in math expressions and with most built-in functions. However, in some cases the exact type of a value is more important. For instance, if <a href="NumPut.htm">NumPut</a> is passed a variable containing a pure integer (not a numeric string), the integer will be used instead of the address of the variable.</p>
<p>To check if a value can be used as a number, use the expression <code><a href="is.htm"><i>value</i> is <i>type</i></a></code>, where <em>type</em> is <code>"number"</code>, <code>"integer"</code>  or <code>"float"</code>.</p>
<p>To check for any type of object, use the expression <code><a href="is.htm"><i>value</i> is 'object'</a></code>.</p>
<p>To check if an object is derived from a particular user-defined class, use <code><a href="is.htm#Objects"><i>value</i> is <i>ClassName</i></a></code>.</p>
<p>For built-in types, the object's class cannot be referenced directly.</p>

<h2 id="Related">相关</h2>
<p><a href="../Concepts.htm#values">Values</a>, <a href="../Language.htm#expressions">Expressions</a>, <a href="is.htm"><em>Value</em> is <em>Type</em></a>, <a href="Integer.htm">Integer</a>, <a href="Float.htm">Float</a>, <a href="String.htm">String</a></p>

<h2 id="Examples">示例</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre>
a := 1, b := 2.0, c := "3"
MsgBox Type(a)  <em>; Integer</em>
MsgBox Type(b)  <em>; Float</em>
MsgBox Type(c)  <em>; String</em>
</pre>
</div>

</body>
</html>